package cn.schoolwow.quickdao.module.database.dml.instance.flow.save;

import cn.schoolwow.quickdao.module.database.dml.instance.service.insert.InsertInstanceArrayCompositeBusiness;
import cn.schoolwow.quickdao.module.database.dml.instance.service.update.UpdateInstanceArrayCompositeBusiness;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

public class SaveInstanceArrayFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        int effect = 0;
        //不存在则插入
        {
            Object[] notExistInstances = (Object[]) flowContext.getData("notExistInstances");
            if(null!=notExistInstances&&notExistInstances.length>0){
                flowContext.startFlow(new InsertInstanceArrayCompositeBusiness())
                        .putTemporaryData("instances", notExistInstances)
                        .execute();
                effect += (Integer)flowContext.checkData("effect");
            }
        }

        //存在则更新
        {
            Object[] existInstances = (Object[]) flowContext.getData("existInstances");
            if(null!=existInstances&&existInstances.length>0){
                flowContext.startFlow(new UpdateInstanceArrayCompositeBusiness())
                        .putTemporaryData("instances", existInstances)
                        .execute();
                effect += (Integer)flowContext.checkData("effect");
            }
        }
        flowContext.putData("effect", effect);
    }

    @Override
    public String name() {
        return "保存实例数组";
    }

}
